﻿using System;
using System.Web.UI.WebControls;
using ltModel;
using Telerik.Web.UI;

public partial class ltMan_links_linkList : AdminBasePage
{
    ltModel.BLL.joins joins = new ltModel.BLL.joins();

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlPager pager = new SqlPager("v_lt_joins_withType", "id", "*");
        if (!string.IsNullOrEmpty(sName.Text))
            pager.Where = " [name] like '%" + sName.Text.Replace("'", "''") + "%'";
        pager.OrderBy = "px asc";
        RadGridBinder.Create(RadGrid1, pager);
    }
    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        int id = int.Parse(GridCommander.ExtractValue(e, "id"));
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "网站链接-删除>" + joins.GetModel(id).name);
        joins.Delete(id);
    }
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Move")
        {
            int id = Convert.ToInt32(GridCommander.ExtractValue(e, "id"));
            joins.PxMove(id, e.CommandArgument.ToString());
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "网站链接-修改顺序>" + joins.GetModel(id).name);
            RadGrid1.Rebind();
        }
    }
    protected void b1_Click(object sender, EventArgs e)
    {
        RadGrid1.Rebind();
    }
    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        ltModel.BLL.joins jbl = new ltModel.BLL.joins();
        ltModel.Model.joins jbo = jbl.GetModel(Int32.Parse(GridCommander.ExtractValue(e, "id")));
        jbo.jid = Int32.Parse(GridCommander.ExtractValue(e, "jid"));
        jbo.name = DataProtector.ToSBC(GridCommander.ExtractValue(e, "name"));
        jbo.link = DataProtector.ToSBC(GridCommander.ExtractValue(e, "link"));
        jbo.pic = DataProtector.ToSBC(GridCommander.ExtractValue(e, "pic"));
        jbl.Update(jbo);
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "网站链接-修改>" + jbo.name);
    }
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        ltModel.BLL.joins jbl = new ltModel.BLL.joins();
        ltModel.Model.joins jbo = new ltModel.Model.joins();
        jbo.id = 0;
        jbo.jid = Int32.Parse(GridCommander.ExtractValue(e, "jid"));
        jbo.name = DataProtector.ToSBC(GridCommander.ExtractValue(e, "name"));
        jbo.link = DataProtector.ToSBC(GridCommander.ExtractValue(e, "link"));
        jbo.pic = DataProtector.ToSBC(GridCommander.ExtractValue(e, "pic"));
        jbo.px = jbl.MaxPx();
        jbl.Add(jbo);
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "网站链接-添加>" + jbo.name);
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            RadComboBox box = e.Item.FindControl("RadComboBox1") as RadComboBox;
            HiddenField hidden = e.Item.FindControl("HiddenField1") as HiddenField;
            box.DataSource = new ltModel.BLL.join_sort().GetAllList().Tables[0];
            box.DataBind();
            box.SelectedValue = hidden.Value;
            box.OnClientSelectedIndexChanged = "function(s,e){$('#" + hidden.ClientID + "').val(s.get_value());}";
            if (string.IsNullOrEmpty(hidden.Value) && box.Items.Count > 0)
                hidden.Value = box.Items[0].Value;
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            RadComboBox box = e.Item.FindControl("RadComboBox1") as RadComboBox;
            HiddenField hidden = e.Item.FindControl("HiddenField1") as HiddenField;
            box.SelectedValue = hidden.Value;
            if (string.IsNullOrEmpty(hidden.Value) && box.Items.Count > 0)
                hidden.Value = box.Items[0].Value;
        }
    }
}